// 数据交换
const apiUrl =
  "https://functions1.memfiredb.com/ckmdhf25g6h95mu41p00/SuperConfig";
function ajax(params) {
  return new Promise((resolve, reject) => {
    let token = store.get("token");
    let q = params.tbName || params.way;
    let url = apiUrl + "?" + q + "-" + params.action;
    $.ajax({
      url,
      type: "POST",
      data: JSON.stringify(params),
      headers: {
        token: token,
        "Content-Type": "application/json",
      },
      success: function (response) {
        // 处理请求成功的逻辑
        resolve(response);
      },
      error: function (xhr, status, error) {
        // 处理请求失败的逻辑
        reject(error);
      },
    });
  });
}
const dalProj = {
  list: function () {
    return ajax({
      way: "comm",
      tbName: "proj",
      action: "list",
      pagesize: 100,
      pageindex: 1,
    });
  },
  insert: async function (title, callback_url) {
    let data=title+new Date().valueOf()+callback_url;
    let params = {
      way: "tools",
      action: "md5",
      data
    };
    let res = await ajax(params);
    if (res.msg != "ok") {
      return res;
    }
    params = {
      way: "comm",
      tbName: "proj",
      action: "save",
      entity: {
        title,
        user_id: store.get("userid"),
        callback_url,
        api_token: res.data,
      },
    };
    res = await ajax(params);
    return res;
  },
  update: function (entity, id) {
    return ajax({
      way: "comm",
      tbName: "proj",
      action: "save",
      entity,
      column: "id",
      value: id,
    });
  },
  del: function (id) {
    return ajax({
      way: "comm",
      tbName: "proj",
      action: "del",
      column: "id",
      value: id,
    });
  },
  notify: async function (data, url) {
    let params = {
      way: "tools",
      action: "httppost",
      data: JSON.stringify(data),
      url,
    };
    // console.log(data);return;
    let res = await ajax(params);
    return res;
  },
};
const dalConfig = {
  list: function () {
    return ajax({
      way: "comm",
      tbName: "config",
      action: "list",
      pageindex: 1,
      pagesize: 100,
    });
  },
  insert: function (entity) {
    return ajax({ way: "comm", tbName: "config", action: "save", entity });
  },
  update: function (entity, id) {
    entity = JSON.parse(JSON.stringify(entity));
    delete entity.id;
    delete entity.created_at;
    return ajax({
      way: "comm",
      tbName: "config",
      action: "save",
      entity,
      column: "id",
      value: id,
    });
  },
  del: function (id) {
    return ajax({
      way: "comm",
      tbName: "config",
      action: "del",
      column: "id",
      value: id,
    });
  },
};
const dalMiniLog = {
  list: function (cat, hotel_id, house_id) {
    let filter = [];
    if (cat) {
      filter.push({ column: "cat", sign: "eq", values: cat });
    }
    if (hotel_id) {
      filter.push({ column: "hotel_id", sign: "eq", values: hotel_id });
    }
    if (house_id) {
      filter.push({ column: "house_id", sign: "eq", values: house_id });
    }
    let params = {
      way: "comm",
      tbName: "mini_log",
      action: "list",
      pagesize: 10,
      filter,
      sorter: [
        {
          column: "id",
          type: "desc",
        },
      ],
    };
    return ajax(params);
  },
  insert: function (entity) {
    return ajax({
      way: "comm",
      tbName: "mini_log",
      action: "save",
      entity,
    });
  },
  del: function (id) {
    return ajax({
      way: "comm",
      tbName: "mini_log",
      action: "del",
      column: "id",
      value: id,
    });
  },
};
const dalUser = {
  refreshToken: async function () {
    let params = {
      way: "user",
      action: "refreshToken",
    };
    let res = await ajax(params);
    if (res.msg == "ok" && res.data.token) {
      store.set("token", res.data.token);
      store.set("userid", res.data.id);
      store.set("username", res.data.username);
      return true;
    }
    return false;
  },
  login: async function (username, password) {
    let params = {
      way: "user",
      action: "login",
      username,
      password,
    };
    let res = await ajax(params);
    if (res.msg == "ok" && res.data.token) {
      store.set("token", res.data.token);
      store.set("username", username);
      store.set("userid", res.data.id);
    }
    return res;
  },
  logout: async function () {
    store.remove("token");
  },
};
